package com.search.controller;

import com.alibaba.fastjson2.JSONObject;
import com.search.entity.BorrowBook;
import com.search.entity.DoubanBook;
import com.search.examples.*;
import com.search.service.BorrowBookService;
import com.search.service.DoubanBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class DoubanBookController {

    @Autowired
    private DoubanBookService doubanBookService;
    @Autowired
    protected BorrowBookService borrowBookService;

    @RequestMapping("/doubanBookPage")
    public JSONObject doubanBookPage(DoubanBook doubanBook){

        JSONObject res = new JSONObject();

        List<DoubanBook> list = doubanBookService.find(doubanBook);
        res.put("data",list);

        return res;
    }

    @RequestMapping("updateBookName")
    public int updateBookName(DoubanBook doubanBook){
        int sum = 0;
        List<DoubanBook> list = new ArrayList<>();
        int page = 1;

        do {doubanBook.setPage(page);
            list= doubanBookService.find(doubanBook);
            page++;
            doubanBook.setPage(page);
            for(DoubanBook book:list){
                String name = book.getBookName();
                try{
                    if(name.contains("：")){
                        String[] arr = name.split("：");
                        book.setBookName(arr[0]);
                        book.setSubtitle(arr[1]);
                    }else if(name.contains(" ")){
                        String[] arr = name.split(" ");
                        book.setBookName(arr[0]);
                        book.setSubtitle(arr[1]);
                    }
                }catch (Exception e){
                    log.error("解析yichang"+book.getId());
                }

                try{
                    doubanBookService.updateNotNullById(book);
                    sum++;
                    log.info(sum+"");
                }catch (Exception e){
                    log.error("更新出错"+book.getId());
                }
            }
        }while (list.size()>0);


        return sum;
    }

    @RequestMapping("searchChaotu")
    public int searchChaotu(DoubanBook doubanBook) {
        int sum = 0;
        List<DoubanBook> list = new ArrayList<>();
        int page = 10;

        do {
            doubanBook.setPage(page);
            log.info("page"+page);
            try {
                list= doubanBookService.find(doubanBook);
            } catch (Exception e) {
                log.error("查询doubanbook出错"+page+e.getMessage());
            } finally {
            }
            page++;
            doubanBook.setPage(page);
            for(DoubanBook resultBook:list){
                String name = resultBook.getBookName();


                int holding = 0;
                try {
                    holding = ChaoTuSearch.search(resultBook.getIsbn().trim(),resultBook.getBookName().trim());
                } catch (Exception e) {
                    log.error("搜索异常，继续"+e.getMessage());
                    continue;
                }
                BorrowBook book = new BorrowBook();
                book.setBookName(resultBook.getBookName().trim());
                book.setDetailUrl(resultBook.getUrl());
                book.setIsbn(resultBook.getIsbn());
                book.setRating(resultBook.getRating());
                book.setListId("");
                book.setClassify("");
                book.setLibrary("chaotu");
                book.setSource("doubanList");
                book.setListName("");
                book.setInsertTime(new Date());
                book.setIsCollect(0);
                book.setEvaluateNum(resultBook.getEvaluateNum());
                book.setHolding(holding);
                try{
                    borrowBookService.saveNotNull(book);
                    sum++;
                }catch (DuplicateKeyException duplicateKeyException ){
                    borrowBookService.updateNotNullById(book);
                }catch (Exception e){
                    log.error("borrowBook插入出错"+e.getMessage()+book.toString());
                }


            }
        }while (list.size()>0);


        return sum;
    }

    @RequestMapping("searchOtherLibaries")
    public int searchOtherLibaries(DoubanBook doubanBook) {


        Runnable run1 = () -> {
            List<DoubanBook> list = new ArrayList<>();
            int page = 0;
            do {
                doubanBook.setPage(page);
                log.info("page"+page);
                try {
                    list= doubanBookService.find(doubanBook);
                } catch (Exception e) {
                    log.error("查询doubanbook出错"+page+e.getMessage());
                } finally {
                }
                page++;
                doubanBook.setPage(page);
                for(DoubanBook resultBook:list){
                    String name = resultBook.getBookName();

                    try {
                        //saveBook("haiDian",resultBook, HaiDianSearch.search(resultBook.getIsbn().trim()));
                        //saveBook("chaotu",resultBook,ChaoTuSearch.search(resultBook.getIsbn().trim(),resultBook.getBookName().trim()));
                        saveBook("daXing",resultBook, DaxingSearch.search(resultBook.getIsbn().trim()));
                        /*saveBook("fangShan",resultBook, FangShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("haiDian",resultBook, HaiDianSearch.search(resultBook.getIsbn().trim()));
                        saveBook("miYun",resultBook, MiYunSearch.search(resultBook.getIsbn().trim()));
                        saveBook("pingGu",resultBook, PingGuSearch.search(resultBook.getIsbn().trim()));
                        saveBook("shiJingShan",resultBook, ShiJingShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("tongZhou",resultBook, TongZhouSearch.search(resultBook.getIsbn().trim()));
                        saveBook("yanQing",resultBook, YanQingSearch.search(resultBook.getIsbn().trim()));*/

                    } catch (Exception e) {
                        log.error("搜索异常，继续"+e.getMessage());
                        continue;
                    }
                }
            }while (list.size()>0);
        };
        new Thread(run1).start();

        /*Runnable run2 = () -> {
            List<DoubanBook> list = new ArrayList<>();
            int page = 2000;
            do {
                doubanBook.setPage(page);
                log.info("page"+page);
                try {
                    list= doubanBookService.find(doubanBook);
                } catch (Exception e) {
                    log.error("查询doubanbook出错"+page+e.getMessage());
                } finally {
                }
                page++;
                doubanBook.setPage(page);
                for(DoubanBook resultBook:list){
                    String name = resultBook.getBookName();

                    try {
                        //saveBook("chaotu",resultBook,ChaoTuSearch.search(resultBook.getIsbn().trim(),resultBook.getBookName().trim()));
                        saveBook("daXing",resultBook, DaxingSearch.search(resultBook.getIsbn().trim()));
                        saveBook("fangShan",resultBook, FangShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("haiDian",resultBook, HaiDianSearch.search(resultBook.getIsbn().trim()));
                        saveBook("miYun",resultBook, MiYunSearch.search(resultBook.getIsbn().trim()));
                        saveBook("pingGu",resultBook, PingGuSearch.search(resultBook.getIsbn().trim()));
                        saveBook("shiJingShan",resultBook, ShiJingShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("tongZhou",resultBook, TongZhouSearch.search(resultBook.getIsbn().trim()));
                        saveBook("yanQing",resultBook, YanQingSearch.search(resultBook.getIsbn().trim()));

                    } catch (Exception e) {
                        log.error("搜索异常，继续"+e.getMessage());
                        continue;
                    }
                }
            }while (list.size()>0 && page<3000);
        };
        new Thread(run2).start();

        Runnable run3 = () -> {
            List<DoubanBook> list = new ArrayList<>();
            int page = 3000;
            do {
                doubanBook.setPage(page);
                log.info("page"+page);
                try {
                    list= doubanBookService.find(doubanBook);
                } catch (Exception e) {
                    log.error("查询doubanbook出错"+page+e.getMessage());
                } finally {
                }
                page++;
                doubanBook.setPage(page);
                for(DoubanBook resultBook:list){
                    String name = resultBook.getBookName();

                    try {
                        //saveBook("chaotu",resultBook,ChaoTuSearch.search(resultBook.getIsbn().trim(),resultBook.getBookName().trim()));
                        saveBook("daXing",resultBook, DaxingSearch.search(resultBook.getIsbn().trim()));
                        saveBook("fangShan",resultBook, FangShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("haiDian",resultBook, HaiDianSearch.search(resultBook.getIsbn().trim()));
                        saveBook("miYun",resultBook, MiYunSearch.search(resultBook.getIsbn().trim()));
                        saveBook("pingGu",resultBook, PingGuSearch.search(resultBook.getIsbn().trim()));
                        saveBook("shiJingShan",resultBook, ShiJingShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("tongZhou",resultBook, TongZhouSearch.search(resultBook.getIsbn().trim()));
                        saveBook("yanQing",resultBook, YanQingSearch.search(resultBook.getIsbn().trim()));

                    } catch (Exception e) {
                        log.error("搜索异常，继续"+e.getMessage());
                        continue;
                    }
                }
            }while (list.size()>0 && page<3875);
        };
        new Thread(run3).start();*/

        /*Runnable run4 = () -> {
            List<DoubanBook> list = new ArrayList<>();
            int page = 3096;
            do {
                doubanBook.setPage(page);
                log.info("page"+page);
                try {
                    list= doubanBookService.find(doubanBook);
                } catch (Exception e) {
                    log.error("查询doubanbook出错"+page+e.getMessage());
                } finally {
                }
                page++;
                doubanBook.setPage(page);
                for(DoubanBook resultBook:list){
                    String name = resultBook.getBookName();

                    try {
                        //saveBook("chaotu",resultBook,ChaoTuSearch.search(resultBook.getIsbn().trim(),resultBook.getBookName().trim()));
                        saveBook("daXing",resultBook, DaxingSearch.search(resultBook.getIsbn().trim()));
                        saveBook("fangShan",resultBook, FangShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("haiDian",resultBook, HaiDianSearch.search(resultBook.getIsbn().trim()));
                        saveBook("miYun",resultBook, MiYunSearch.search(resultBook.getIsbn().trim()));
                        saveBook("pingGu",resultBook, PingGuSearch.search(resultBook.getIsbn().trim()));
                        saveBook("shiJingShan",resultBook, ShiJingShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("tongZhou",resultBook, TongZhouSearch.search(resultBook.getIsbn().trim()));
                        saveBook("yanQing",resultBook, YanQingSearch.search(resultBook.getIsbn().trim()));

                    } catch (Exception e) {
                        log.error("搜索异常，继续"+e.getMessage());
                        continue;
                    }
                }
            }while (list.size()>0 && page<3496);
        };
        new Thread(run4).start();

        Runnable run5 = () -> {
            List<DoubanBook> list = new ArrayList<>();
            int page = 3496;
            do {
                doubanBook.setPage(page);
                log.info("page"+page);
                try {
                    list= doubanBookService.find(doubanBook);
                } catch (Exception e) {
                    log.error("查询doubanbook出错"+page+e.getMessage());
                } finally {
                }
                page++;
                doubanBook.setPage(page);
                for(DoubanBook resultBook:list){
                    String name = resultBook.getBookName();

                    try {
                        //saveBook("chaotu",resultBook,ChaoTuSearch.search(resultBook.getIsbn().trim(),resultBook.getBookName().trim()));
                        saveBook("daXing",resultBook, DaxingSearch.search(resultBook.getIsbn().trim()));
                        saveBook("fangShan",resultBook, FangShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("haiDian",resultBook, HaiDianSearch.search(resultBook.getIsbn().trim()));
                        saveBook("miYun",resultBook, MiYunSearch.search(resultBook.getIsbn().trim()));
                        saveBook("pingGu",resultBook, PingGuSearch.search(resultBook.getIsbn().trim()));
                        saveBook("shiJingShan",resultBook, ShiJingShanSearch.search(resultBook.getIsbn().trim()));
                        saveBook("tongZhou",resultBook, TongZhouSearch.search(resultBook.getIsbn().trim()));
                        saveBook("yanQing",resultBook, YanQingSearch.search(resultBook.getIsbn().trim()));

                    } catch (Exception e) {
                        log.error("搜索异常，继续"+e.getMessage());
                        continue;
                    }
                }
            }while (list.size()>0 && page<3876);
        };
        new Thread(run5).start();*/


        return 0;
    }

    private void saveBook(String library,DoubanBook resultBook,int holding){
        BorrowBook book = new BorrowBook();
        book.setBookName(resultBook.getBookName().trim());
        book.setDetailUrl(resultBook.getUrl());
        book.setIsbn(resultBook.getIsbn());
        book.setRating(resultBook.getRating());
        book.setListId("");
        book.setClassify("");
        book.setLibrary(library);
        book.setSource("doubanList");
        book.setListName("");
        book.setInsertTime(new Date());
        book.setIsCollect(0);
        book.setEvaluateNum(resultBook.getEvaluateNum());
        book.setHolding(holding);
        try{
            borrowBookService.saveNotNull(book);
            //sum++;
        }catch (DuplicateKeyException duplicateKeyException ){
            borrowBookService.updateNotNullById(book);
        }catch (Exception e){
            log.error("borrowBook插入出错"+e.getMessage()+book.toString());
        }
    }


}
